class SingleLinkedList {
    static class ListNode{
        public int val;
        public ListNode next;

        public ListNode(int val) {
            this.val = val;
        }
    }
    public ListNode head;

    //头插法
    public void addFirst(int val){
        ListNode node = new ListNode(val);
        node.next = head;
        head = node;
    }
    //尾插法
    public void addLast(int val){
        ListNode node = new ListNode(val);
        if(head == null){
            head = node;
            return;
        }
        ListNode cur = head;
        while (cur.next != null){
            cur = cur.next;
        }
        cur.next = node;
    }
    //任意位置插入,第一个数据节点为0号下标
    public void addIndex(int index,int val){
        try {
            checkIndex(index);
        }catch (IndexNotLegalException e){
            e.printStackTrace();
        }
        if(index == 0){
            addFirst(val);
            return;
        }
        if(index == size()){
            addLast(val);
            return;
        }
        ListNode cur = findIndexSubOne(index);

        ListNode node = new ListNode(val);
        node.next = cur.next;
        cur.next = node;
    }

    private void checkIndex(int index) throws IndexNotLegalException{
        if(index < 0 || index > size()){
            throw new IndexNotLegalException("index不合法！");
        }
    }

    private ListNode findIndexSubOne(int index){
        int count = 0;
        ListNode cur = head;
        while(count != index-1){
            cur = cur.next;
            count++;
        }
        return cur;
    }

    //查找是否包含关键字key是否在单链表当中
    public boolean contains(int val){
        ListNode cur = head;
        while(cur != null){
            if(cur.val == val){
                return true;
            }
            cur = cur.next;
        }
        return false;
    }
    //删除第一次出现关键字为key的节点
    public void remove(int val){
        if(head == null){
            return;
        }
        if(head.val == val){
            head = head.next;
            return;
        }
        ListNode cur = head;
        while(cur.next != null){
            if(cur.next.val == val){
                ListNode del = cur.next;
                cur.next = del.next;
                return;
            }
            cur = cur.next;
        }
    }
    //删除所有值为key的节点
    public void removeAllKey(int val){
        //1. 判空
        if(this.head == null) {
            return;
        }
        //2. 定义prev 和 cur
        ListNode prev = head;
        ListNode cur = head.next;
        //3.开始判断并且删除
        while(cur != null) {
            if(cur.val == val) {
                prev.next = cur.next;
                //cur = cur.next;
            }else {
                prev = cur;//prev = prev.next;
                //cur = cur.next;
            }
            cur = cur.next;
        }
        //4.处理头节点
        if(head.val == val) {
            head = head.next;
        }
    }
    //得到单链表的长度
    public int size(){
        int count = 0;
        ListNode cur = head;
        while(cur != null){
            count++;
            cur = cur.next;
        }
        return count;
    }

    public void display(){
        ListNode cur = head;
        while(cur != null){
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
        System.out.println();
    }

    public void display(ListNode newHead){
        ListNode cur = newHead;
        while(cur != null){
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
        System.out.println();
    }

    public void clear(){
        ListNode cur = head;
        while(cur != null){
            ListNode curN = cur.next;
            cur = null;
            cur = curN;
        }
        head = null;
    }

    public ListNode removeElements(int val){
        if(head == null){
            return null;
        }
        ListNode prev = head;
        ListNode cur = head.next;

        while(cur != null){
            if(cur.val == val){
                prev.next = cur.next;
            }else{
                prev = cur;
            }
            cur = cur.next;
        }
        if(head.val == val){
            head = head.next;
        }
        return head;
    }

    public ListNode reverseList(){
        if(head == null){
            return head;
        }
      ListNode cur = head.next;
      head.next = null;

      while(cur != null){
          ListNode curN = cur.next;

          cur.next = head;
          head = cur;
          cur = curN;
      }
      return head;
    }

    public ListNode middleVal() {
        ListNode fast = head;
        ListNode slow = head;

        while (fast != null && fast.next != null){
            fast = fast.next.next;
            slow = slow.next;
        }
        return slow;
    }

    public int kthToLast(int k){
        if(k <= 0 || head == null){
            return -1;
        }
        ListNode fast = head;
        ListNode slow = head;
        int count = 0;
        while(count != k-1){
            fast = fast.next;
            count++;
            if(fast == null){
                return -1;
            }
        }
        while (fast.next != null){
            fast = fast.next;
            slow = slow.next;
        }
        return slow.val;
    }

    public static ListNode partition(ListNode pHead, int x) {
        // write code here
        if(pHead == null){
            return null;
        }

        ListNode bs = null;
        ListNode be = null;
        ListNode as = null;
        ListNode ae = null;
        ListNode cur = pHead;

        while (cur != null){
            if(cur.val < x){
                //判断第一次插入
                if(bs == null){
                    bs = be = cur;
                }else{
                    be.next = cur;
                    be = be.next;
                }
            }else{
                //判断第一次插入
                if(as == null){
                    as = ae =cur;
                }else{
                    ae.next = cur;
                    ae = ae.next;
                }
            }
            cur = cur.next;
        }
        //bs为空
        if(bs == null){
            return as;
        }
        be.next = as;
        if(ae != null){
            ae.next = null;
        }
        return bs;
    }

}
